package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wanho.common.Constants;
import net.wanho.po.Product;
import net.wanho.service.ProductService;
import net.wanho.mapper.ProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
* @author Administrator
* @description 针对表【t_product(产品表)】的数据库操作Service实现
* @createDate 2025-07-03 14:46:23
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Override
    public void saveProduct(Product product) {
        product.setState(Constants.PRODUCT_STATUS_NOT_PASS); // 僵尸代码,可读性差
        product.setIsDel(0);
        save(product);
    }

    @Override
    public List<Product> find(Product product) {
        // 定义条件构造器, 用来封装查询条件
        // QueryWrapper<Product> wrapper = new QueryWrapper<>();
        // wrapper.like(!ObjectUtils.isEmpty(product.getName()), "pro_name", product.getName());  // pro_name like '%name%'
        // wrapper.or();
        // wrapper.eq("pro_price", product.getPrice());
        // wrapper.eq(!ObjectUtils.isEmpty(product.getState()), "pro_state", product.getState());
        // wrapper.orderByDesc("pro_id");

        // 使用Lambda语法, 使用方法引用,避免书写错误
        // LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        // wrapper.like(!ObjectUtils.isEmpty(product.getName()), Product::getName, product.getName());
        // wrapper.eq(!ObjectUtils.isEmpty(product.getPrice()), Product::getPrice, product.getPrice());

        // List<Product> list = this.list(wrapper);

        List<Product> list = this.list(Wrappers.<Product>lambdaQuery().like(Product::getName, product.getName()));

        return list;
    }

    @Override
    public Page<Product> findPage(int pageNum, int pageSize, Product product) {
        // 1.创建分页对象, 指定页码和页大小
        Page<Product> page = new Page<>(pageNum, pageSize);

        // 2.查询
        LambdaQueryWrapper<Product> wrapper = Wrappers.<Product>lambdaQuery().like(Product::getName, product.getName());
        Page<Product> p = this.page(page, wrapper);
        // System.out.println(p == page); // true

        return page;
    }
}




